package com.hhit.collaborative_learning.utils;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

public class Utils {
	public static String sevenDaysAgo = "";
	public static String twoDaysAgo = "";
	public static String currentTime = "";
	
	public static String expertSevenDaysAgo="";
	public static String expertTwoDaysAgo="";
	public static String expertCurrentTime="";
	
	public static String personalCurrentTime="";
	
	public static String commentCurrentTime="";
	
	public static String blogRightCurrentTime="";
	public static String blogRightOneDaysAge="";
	
	public static Calendar now = null;

	public static String saveImage(String path, Part img) {
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdir();
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String now = sdf.format(date);
		File file = new File(dir, now + ".jpg");
		try {
			img.write(file.getAbsolutePath());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return now + ".jpg";
	}

	public static boolean deleteImage(String absPath) {
		File file = new File(absPath);
		if (file.exists() && file.isFile()) {
			return file.delete();
		}
		return false;
	}

	public static void getSomedaysAgo(Calendar now) {
		if (now == null) {
			now = Calendar.getInstance();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		now.add(Calendar.DATE, -7);
		sevenDaysAgo = sdf.format(now.getTime());
		now.add(Calendar.DATE, 5);
		twoDaysAgo = sdf.format(now.getTime());
		now.add(Calendar.DATE, 2);
		currentTime = sdf.format(now.getTime());
	}
	
	public static void getBlogRightDaysAge(Calendar now){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		now.add(Calendar.DATE, -1);
		blogRightOneDaysAge = sdf.format(now.getTime());
		now.add(Calendar.DATE, 1);
		blogRightCurrentTime = sdf.format(now.getTime());
	}
	
	public static String getSomedaysAgo2(Calendar now) {
		if (now == null) {
			now = Calendar.getInstance();
		}
		StringBuilder sb=new StringBuilder("");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		now.add(Calendar.DATE, -7);
		sb.append(sdf.format(now.getTime())+";");
//		sevenDaysAgo = sdf.format(now.getTime());
		now.add(Calendar.DATE, 5);
		sb.append(sdf.format(now.getTime())+";");
//		twoDaysAgo = sdf.format(now.getTime());
		now.add(Calendar.DATE, 2);
		sb.append(sdf.format(now.getTime()));
//		currentTime = sdf.format(now.getTime());
		return sb.toString();
	}

	public static void main(String[] args) {
//		Date date = new Date();
		// System.out.println("date:"+date.getYear()+"-"+date.getMonth()+"-"+date.getDate());
//		Calendar cal = Calendar.getInstance();
//		cal.set(Calendar.MONTH, 11);
//		System.out.println(cal.getActualMaximum(Calendar.DATE));
//		 System.out.println("Cal:"+cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE));
		// Calendar now1=Calendar.getInstance();
		// now1.add(Calendar.DATE, -7);
		// Date date=now1.getTime();
		// System.out.println(date.toLocaleString());
		// now1.add(Calendar.DATE, 5);
		// date=now1.getTime();
		// System.out.println(date.toLocaleString());
//		System.out.println(5/5.0);
		 System.out.println(getRelativeTime("2015-04-30 00:18:00"));
		
	}
	
	public static String getRelativeName(String name){
		if(name.equals("mobile")){
			return "移动开发";
		}else if(name.equals("web")){
			return "Web前端";
		}else if(name.equals("enterprise")){
			return "构架设计";
		}else if(name.equals("code")){
			return "编程语言";
		}else if(name.equals("www")){
			return "互联网";
		}else if(name.equals("database")){
			return "数据库";
		}else if(name.equals("system")){
			return "系统运营";
		}else if(name.equals("cloud")){
			return "云计算";
		}else if(name.equals("software")){
			return "研发管理";
		}else{
			return "综合";
		}
	}

	public static String getRelativeTime(String time) {
		String relativetime = "";
		if (now == null) {
			now = Calendar.getInstance();
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");
			Date giveTime = sdf.parse(time);
			Calendar giveTimeCal = Calendar.getInstance();
			giveTimeCal.setTime(giveTime);
			int giveYear = giveTimeCal.get(Calendar.YEAR);
			int giveMonth = giveTimeCal.get(Calendar.MONTH) + 1;
			int giveDay = giveTimeCal.get(Calendar.DAY_OF_MONTH);
			int nowYear = now.get(Calendar.YEAR);
			int nowMonth = now.get(Calendar.MONTH) + 1;
			int nowDay = now.get(Calendar.DAY_OF_MONTH);
			if (nowYear - giveYear == 0) {
				if (nowMonth - giveMonth == 0) {
					if (nowDay - giveDay == 0) {
						long giveMs=giveTimeCal.getTimeInMillis();
						long nowMs=now.getTimeInMillis();
						long differHour=(nowMs-giveMs)/(60*60*1000);
						if(differHour>0){
							relativetime=differHour+"小时前";
						}else{
							long differMin=(nowMs-giveMs)/(60*1000);
							if(differMin>0){
								relativetime=differMin+"分钟前";
							}else{
								relativetime="刚刚";
							}
						}
					} else if (nowDay - giveDay == 1) {
						relativetime = "昨天 " + sdf1.format(giveTime);
					} else if (nowDay - giveDay == 2) {
						relativetime = "前天 " + sdf1.format(giveTime);
					} else if(nowDay - giveDay >2&&nowDay - giveDay <7){
						relativetime = (nowDay - giveDay) + "天前 "
								+ sdf1.format(giveTime);
					}else{
						relativetime=time;
					}
				} else if (nowMonth - giveMonth == 1) {
					if (nowDay + giveTimeCal.getActualMaximum(Calendar.DATE)
							- giveDay == 1) {
						relativetime = "昨天 " + sdf1.format(giveTime);
					} else if (nowDay
							+ giveTimeCal.getActualMaximum(Calendar.DATE)
							- giveDay == 2) {
						relativetime = "前天 " + sdf1.format(giveTime);
					} else if((nowDay
							+ giveTimeCal.getActualMaximum(Calendar.DATE) - giveDay)>2&&(nowDay
									+ giveTimeCal.getActualMaximum(Calendar.DATE) - giveDay)<7){
						relativetime = (nowDay
								+ giveTimeCal.getActualMaximum(Calendar.DATE) - giveDay)
								+ "天前 " + sdf1.format(giveTime);
					}else{
						relativetime=time;
					}
				}
			} else if (nowYear - giveYear == 1) {
				if(nowMonth-giveMonth==-11){
					if (nowDay + 31 - giveDay == 1) {
						relativetime = "昨天 " + sdf1.format(giveTime);
					} else if (nowDay + 31 - giveDay == 2) {
						relativetime = "前天 " + sdf1.format(giveTime);
					} else if(nowDay + 31 - giveDay>2&&nowDay + 31 - giveDay<7){
						relativetime = (nowDay + 31 - giveDay) + "天前 "
								+ sdf1.format(giveTime);
					}else{
						relativetime=time;
					}
				}else{
					relativetime=time;
				}
			}else{
				relativetime=time;
			}

			// long differDay=(nowMs-giveTimeMs)/(24*60*60*1000);
			// if(differDay==0){
			// long differHour=(nowMs-giveTimeMs)/(60*60*1000);
			// if(differHour==0){
			// long differMin=(nowMs-giveTimeMs)/(60*1000);
			// if(differMin==0){
			// relativetime="刚刚";
			// }else{
			// relativetime=differMin+"分钟前";
			// }
			// }else{
			// relativetime=differHour+"小时前";
			// }
			// }else if(differDay==1){
			// relativetime="昨天 "+sdf1.format(giveTime);
			// }else if(differDay==2){
			// relativetime="前天 "+sdf1.format(giveTime);
			// }else{
			// relativetime=differDay+"天前 "+sdf1.format(giveTime);
			// }
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return relativetime;
	}
	
	public static String getCurrentTime(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}
	
	public static String getCookieValue(String name,HttpServletRequest request){
		String value="";
		Cookie[] cookies=request.getCookies();
		for(Cookie cookie:cookies){
			if(cookie.getName().equals(name)){
				value+=cookie.getValue();
				break;
			}
		}
		return value;
	}
	public static String getDataRelativeName(String name){
		if(name.equals("mobile_development")){
			return "移动开发";
		}else if(name.equals("development_technology")){
			return "开发技术";
		}else if(name.equals("course_resource")){
			return "课程资源";
		}else if(name.equals("network_technology")){
			return "网络技术";
		}else if(name.equals("operating_system")){
			return "操作系统";
		}else if(name.equals("security_technology")){
			return "安全技术";
		}else if(name.equals("database")){
			return "数据库";
		}else if(name.equals("industry")){
			return "行业";
		}else if(name.equals("server")){
			return "服务器应用";
		}else if(name.equals("store")){
			return "存储";
		}else if(name.equals("information")){
			return "信息化";
		}else{
			return "考试认证";
		}
	}

}
